<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 第一行表头 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span><strong> 江苏传智播客教育科技股份有限公司</strong></span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col><strong>负责人</strong></el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体内容 -->
        <el-tree
          icon-class=" "
          :data="list"
          default-expand-all
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data, node }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <i v-if="!data.children.length" class="el-icon-user" />
                <i v-else :class="node.expanded ? 'el-icon-remove-outline' : 'el-icon-circle-plus-outline' " />
                <span> {{ data.name }}</span>
                <!-- <span>{{ node }}</span> -->
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- 没有子部门则显示删除部门 -->
                        <el-dropdown-item v-if="!data.children.length" @click.native="doDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      @close="$refs.dialogRef.resetForm()"
    >
      <deptDialog
        :id="curId"
        ref="dialogRef"
        :is-edit="isEdit"
        :origin-list="originList"
        @closeWindow="dialogFormVisible = false; getDepartmentList()"
      />
    </el-dialog>
  </div>
</template>

<script>
import { deleteDepartment, getDepartments } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index.js'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      // 编辑弹窗显示
      isEdit: false,
      curId: '',
      list: [], // 部门列表
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogFormVisible: false,
      originList: []
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门列表
    async getDepartmentList() {
      const res = await getDepartments()
      console.log(res)
      this.list = res.data.depts
      this.list.shift()

      this.originList = this.list.map(item => ({
        id: item.id,
        code: item.code,
        pid: item.pid,
        name: item.name
      }))
      console.log('originList', this.originList)

      this.list = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 添加子部门
    doAdd(id) {
      this.dialogFormVisible = true
      this.curId = id
      this.isEdit = false
    },
    // 删除部门
    async doDelete(id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteDepartment(id)
        this.$message.success('删除成功')
        this.getDepartmentList()
      })
    },
    // 编辑部门
    async doEdit(id) {
      this.isEdit = true
      this.dialogFormVisible = true
      this.curId = id

      // 调用子组件的方法，数据回显
      await this.$nextTick()
      this.$refs.dialogRef.loadDepartmentDetails()
    }
  }
}
</script>

<style>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
